<?php

// Application/View/Helper/ControllerName.php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class Script extends AbstractHelper
{

    protected $routeMatch;
    protected $pathJs;
    public $init;

    public function __construct( $routeMatch )
    {
        $this->routeMatch = $routeMatch;
        $this->pathJs     = $_SERVER['DOCUMENT_ROOT'] . DS . 'js' . DS . 'scripts' . DS . 'module';

    }

    public function __invoke()
    {
        if ($this->routeMatch) {
            $urlController = $this->routeMatch->getParam( 'controller', 'index' );
            $action        = $this->routeMatch->getParam( 'action', 'index' );
            $arrUrl        = explode( "\\", $urlController );

            $module     = strtolower( $arrUrl[0] );
            $controller = strtolower( $arrUrl[2] );

            echo $this->_ModuleControllerAction( $module, $controller, $action );
        }

    }

    private function _ModuleControllerAction( $module, $controller, $action )
    {
        $scriptInicial = " var  baseRequest = { " . PHP_EOL;
        $scriptInicial .= "   \"module\"     : \"{$module}\", " . PHP_EOL;
        $scriptInicial .= "   \"controller\" : \"{$controller}\", " . PHP_EOL;
        $scriptInicial .= "   \"action\"     : \"{$action}\" " . PHP_EOL;
        $scriptInicial .= "}; " . PHP_EOL;
        $headScript    = new \Zend\View\Helper\HeadScript();
        return $headScript->prependScript( $scriptInicial );

    }

}
